package com.pda.common.pages.main;

import android.os.Bundle;
import android.view.View;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import com.pda.common.R;
import com.pda.common.basemvp.activity.BaseActivity_noMVP;
import com.pda.common.bean.LoginBean;
import com.pda.common.database.DataBaseControl;
import com.pda.common.tools.SharedPreferencesManger;
import com.pda.common.utils.PDAUtil;
import com.shelwee.update.UpdateHelper;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class MainActivity extends BaseActivity_noMVP {
    private String TAG = "MainActivity";
    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tvright)
    TextView tvright;
    @BindView(R.id.gridview)
    GridView gridview;
    List<LoginBean.DataBean.ShowInfoBean> list;

    @Override
    protected void initdata() {
        tvTitle.setText("主页");
        list =  (List<LoginBean.DataBean.ShowInfoBean>)getIntent().getSerializableExtra(PDAUtil.keyUF);

        MainAdapter mainAdapter = new MainAdapter(this,list);
        gridview.setAdapter(mainAdapter);

        new Thread(new Runnable() {
            @Override
            public void run() {
                if(DataBaseControl.getCodeAllType(PDAUtil.RELATION).size()>500)
                {
                    DataBaseControl.deleteCodes(DataBaseControl.getCodeAllType(PDAUtil.RELATION));
                }
                if(DataBaseControl.getCodeAllType(PDAUtil.IN).size()>500)
                {
                    DataBaseControl.deleteCodes(DataBaseControl.getCodeAllType(PDAUtil.IN));
                }
                if(DataBaseControl.getCodeAllType(PDAUtil.OUT).size()>500)
                {
                    DataBaseControl.deleteCodes(DataBaseControl.getCodeAllType(PDAUtil.OUT));
                }
            }
        }).start();

        UpdateHelper updateHelper = new UpdateHelper.Builder(this)
                .checkUrl("http://"+ SharedPreferencesManger.getHostName()+"/pda/APPSoftUpdate.ashx")
                .isAutoInstall(true)
                .build();
        updateHelper.check();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initdata();
    }

    @OnClick({R.id.iv_title_left})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_title_left:
                finish();
                break;
        }
    }
}
